package com.charge.web;

import com.charge.domain.Account;
import com.charge.domain.User;
import com.charge.domain.UserIncomeType;
import com.charge.domain.UserSpendingType;
import com.charge.dto.ReportDTO;
import com.charge.service.*;
import com.charge.vo.ReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description :
 * Author : 91658
 * Date : 2018/6/11 17:27
 **/
@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private IncomeService incomeService;

    @Autowired
    private SpendingService spendingService;

    @Autowired
    private ReportService reportService;

    @RequestMapping("/report_manage")
    public String reportManage(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Account account = new Account();
        account.setUserId(user.getId());
        List<Account> accountList = accountService.selectAccountList(account);
        request.setAttribute("accountList", accountList);

        UserSpendingType userSpendingType = new UserSpendingType();
        userSpendingType.setUserId(user.getId());
        List<UserSpendingType> userSpendingTypeList = spendingService.selectSpendingList(userSpendingType);
        request.setAttribute("spendingList", userSpendingTypeList);

        UserIncomeType userIncomeType = new UserIncomeType();
        userIncomeType.setUserId(user.getId());
        List<UserIncomeType> userIncomeTypeList = incomeService.selectIncomeList(userIncomeType);
        request.setAttribute("incomeList", userIncomeTypeList);
        return "/report/report_manage";
    }

    @RequestMapping("/report_search")
    @ResponseBody
    public Map<String, List<ReportVO>> reportSearch(ReportDTO reportDTO, HttpSession session) {
        User user = (User) session.getAttribute("user");
        reportDTO.setUserId(user.getId());
        Map<String, List<ReportVO>> map = new HashMap<>();
        Integer[] dimensionArray = reportDTO.getDimension();
        for (Integer dimension : dimensionArray) {
            if (dimension == 1) {
                List<ReportVO> annualReport = reportService.annualReport(reportDTO);
                map.put("annualReport", annualReport);
            } else if (dimension == 2) {
                List<ReportVO> quarterReport = reportService.quarterReport(reportDTO);
                map.put("quarterReport", quarterReport);
            } else if (dimension == 3) {
                List<ReportVO> monthlyReport = reportService.monthlyReport(reportDTO);
                map.put("monthlyReport", monthlyReport);
            } else if (dimension == 4) {
                List<ReportVO> weekReport = reportService.weekReport(reportDTO);
                map.put("weekReport", weekReport);
            } else if (dimension == 5) {
                List<ReportVO> weekDateReport = reportService.weekDateReport(reportDTO);
                map.put("weekDateReport", weekDateReport);
            } else if (dimension == 6) {
                List<ReportVO> accountReport = reportService.accountReport(reportDTO);
                map.put("accountReport", accountReport);
            } else if (dimension == 7) {
                List<ReportVO> projectReport = reportService.projectReport(reportDTO);
                map.put("projectReport", projectReport);
            } else if (dimension == 8) {
                List<ReportVO> daysReport = reportService.daysReport(reportDTO);
                map.put("daysReport", daysReport);
            }
        }
        return map;
    }

}
